<?php

declare(strict_types=1);

namespace App\Job;

use App\Model\ChargeDetail;
use App\Model\Device;
use App\Model\Normal;
use App\Model\Order;
use App\Services\Iot\IotService1 as IotService;
use Hyperf\AsyncQueue\Job;
use Hyperf\AsyncQueue\Driver\DriverFactory;
use Hyperf\Context\ApplicationContext;
use App\Job\CheckJob;

class OrderJob extends Job
{
    public string $orderNum;


    /**
     * 任务执行失败后的重试次数，即最大执行次数为 $maxAttempts+1 次
     */
    protected int $maxAttempts = 2;

    public function __construct($orderNum)
    {
        // 这里最好是普通数据，不要使用携带 IO 的对象，比如 PDO 对象
        $this->orderNum = $orderNum;
    }

    public function handle()
    {
        $orderModel = Order::where('number', $this->orderNum)->first();
        if (!$orderModel) {
            Normal::logger('订单' . $this->orderNum . '不存在', 'order-queue', 'error');
            return '';
        }
        try{
            if ($orderModel->status == Order::STATUS_USING) {
                $imei = Device::where('id', $orderModel->device_id)->value('iot_sn');
                if (!$imei) {
                    Normal::logger('订单' . $this->orderNum . '设备号不存在', 'order-queue', 'error');
                    return '';
                }
                if ($orderModel->power <= 0) {
                    if ($orderModel->is_autoload <= 0) {
                        $minutes = ChargeDetail::where('id', $orderModel->charge_detail_id)
                            ->value('minutes');
                        $orderModel->minutes = $minutes;
//                        if($minutes > 720){
//                             $minutes = 720;
//                        }
                    }else{
                        $minutes =  round($orderModel->original_fee * 240);
                        $orderModel->minutes = $minutes;
                    }
                    $orderModel->is_check = 1;
                    $orderModel->save();
                    if ($minutes > 10) {
                        IotService::start($imei, $orderModel->channel_number, $minutes - 10, 0);
                    }
                } else {
                    $detailList = ChargeDetail::where('charge_option_id', $orderModel->charge_id)
                        ->where('is_del',1)
                        ->get()->toArray();
                    $minutes = ChargeDetail::where('id', $orderModel->charge_detail_id)
                        ->value('minutes');
                    foreach ($detailList as $k => $v) {
                        if ($orderModel->power >= $v['min_power'] && $orderModel->power <= $v['max_power']) {
                            $minutes = $v['minutes'];
                            break;
                        }
                    }
                    $finalMinutes = round($orderModel->original_fee * $minutes);
//                    if($finalMinutes > 720){
//                        $finalMinutes = 720;
//                    }
                    $orderModel->minutes = $finalMinutes;
                    $orderModel->is_check = 1;
                    $orderModel->save();
                    if ($finalMinutes > 10) {
                        IotService::start($imei, $orderModel->channel_number, (int)$finalMinutes - 10, 0);
                    }
                }
                try{
                    $driver = ApplicationContext::getContainer()->get(DriverFactory::class)->get('check-stop');
//                    if($orderModel->is_autoload <= 0){
                        $driver->push(new CheckJob($this->orderNum), (int)$orderModel->minutes*60);
//                    }else{
//                        $driver->push(new CheckJob($this->orderNum), $finalMinutes*60);
//                    }
                }catch (\Exception $e){
                    Normal::logger('订单' . $this->orderNum . '异常--'.$e->getMessage(), 'order-queue', 'error');
                }
            }
        }catch (\Exception $e){
            Normal::logger('订单开启' . $this->orderNum . '异常--'.$e->getMessage(), 'order-queue', 'error');
        }
    }
}
